#!/bin/bash
# EventPrint Studio for macOS: starts the program's server (if it is not running yet) and opens the
# program in its own Chrome or Edge window. The server stops by itself a few minutes after the
# program window is closed.
RES="$(cd "$(dirname "$0")/../Resources" && pwd)"
PORT=3077
URL="http://127.0.0.1:${PORT}/"
DATA="$HOME/Library/Application Support/EventPrint Studio"
mkdir -p "$DATA"

# The bundle was approved by the user ("Open Anyway"); its own files no longer carry the download
# mark, so the image library inside is not stopped by the system again.
/usr/bin/xattr -dr com.apple.quarantine "$RES" 2>/dev/null

# The server still answers with the program's original internal name.
radi() { /usr/bin/curl -s -m 3 "${URL}api/zdravlje" | /usr/bin/grep -q '"program":"NatpisFotoSoft"'; }

# A server left running by another version of the program (the copy that was open before an
# update) is stopped first, so this copy never opens on the old version's server.
VERZIJA="$(/usr/bin/sed -n 's/.*"version": *"\([^"]*\)".*/\1/p' "$RES/server/package.json" | /usr/bin/head -1)"
pokrenuta() { /usr/bin/curl -s -m 3 "${URL}api/zdravlje" | /usr/bin/sed -n 's/.*"verzija":"\([^"]*\)".*/\1/p'; }
if radi && [ -n "$VERZIJA" ] && [ "$(pokrenuta)" != "$VERZIJA" ]; then
  for PID in $(/usr/sbin/lsof -nP -iTCP:${PORT} -sTCP:LISTEN -t 2>/dev/null); do
    case "$(/bin/ps -p "$PID" -o comm= 2>/dev/null)" in
      */node|node) /bin/kill "$PID" 2>/dev/null ;;
    esac
  done
  for i in $(seq 1 40); do /usr/bin/curl -s -m 1 -o /dev/null "$URL" 2>/dev/null || break; sleep 0.25; done
fi

if ! radi; then
  if /usr/bin/curl -s -m 2 -o /dev/null "${URL}" 2>/dev/null; then
    /usr/bin/osascript -e "display alert \"EventPrint Studio se nije pokrenuo\" message \"Na adresi 127.0.0.1:${PORT} već radi drugi program. Zatvorite ga ili ponovo pokrenite računar.\""
    exit 1
  fi
  cd "$RES/server" || exit 1
  nohup "$RES/node/node" server.js >>"$DATA/server.log" 2>&1 &
  for i in $(seq 1 80); do radi && break; sleep 0.25; done
fi
if ! radi; then
  /usr/bin/osascript -e "display alert \"EventPrint Studio se nije pokrenuo\" message \"Opis greške je u fajlu: ${DATA}/greske.log\""
  exit 1
fi

# The program was earlier called NatpisFotoSoft. Its data are taken over by the server; the old
# application itself is not needed any more, and the user is told once that it can be removed.
if [ ! -e "$DATA/stara-aplikacija-javljeno" ]; then
  for S in "/Applications/NatpisFotoSoft.app" "$HOME/Applications/NatpisFotoSoft.app"; do
    if [ -d "$S" ]; then
      /usr/bin/osascript -e "display alert \"Program sada nosi ime EventPrint Studio\" message \"Podešavanja, natpisi i licenca su preneti. Staru aplikaciju NatpisFotoSoft iz foldera Aplikacije možete prevući u Otpad.\"" >/dev/null 2>&1
      break
    fi
  done
  : > "$DATA/stara-aplikacija-javljeno"
fi

PROFIL="$DATA/edge-profil"
ARGS=(--app="$URL" --user-data-dir="$PROFIL" --no-first-run --no-default-browser-check --disable-extensions --disable-sync
      --disable-background-timer-throttling --disable-renderer-backgrounding --disable-backgrounding-occluded-windows
      --disable-features=Translate,TranslateUI)
for B in "Google Chrome" "Microsoft Edge" "Brave Browser" "Chromium"; do
  if /usr/bin/open -Ra "$B" 2>/dev/null; then
    /usr/bin/open -na "$B" --args "${ARGS[@]}"
    exit 0
  fi
done
/usr/bin/osascript -e 'display alert "Preporučen je Google Chrome ili Microsoft Edge" message "EventPrint Studio najbolje radi u Chrome-u ili Edge-u. Sada se otvara u podrazumevanom pregledaču."'
/usr/bin/open "$URL"
